<?php

namespace app\modules\admin\components;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use app\components\Api;
use app\components\Wechat;
use app\components\Loyalty;

class BaseAdminController extends Controller
{
    /**
     * @var string the ID of the action that is used when the action ID is not specified
     * in the request. Defaults to 'index'.
     */
    public $defaultAction = 'index';

    /**
     * @var null|string|false the name of the layout to be applied to this controller's views.
     * This property mainly affects the behavior of [[render()]].
     * Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value.
     * If false, no layout will be applied.
     */
    public $layout = 'admin';

    /**
     * 
     */
    public function init()
    {
        
    }

    /**
     * 断点调试
     * @param mix $data
     * @param boolean $break
     */
    public function bp($data, $break = true)
    {
        echo "<pre>";
        if (is_string($data)) echo $data; else print_r($data);
        echo "</pre>";
        if ($break) exit;
    }

    /**
     * use our ErrorController to display error
     * @param string $message
     **/
    public function error($message, $code = null)
    {
        $this->redirect(['errors/index', 'message' => $message, 'code' => $code]);
    }

    /**
     * set or get session value based on Yii::$app->session
     */
    public function session($key, $value = null)
    {
        $session = Yii::$app->session;

        // open session
        if (!$session->isActive) {
            $session->open();
        }

        if ($session->has($key)) {

            // return the existing key when value is null
            if (is_null($value)) {
                return $session->get($key);
            }

            // replace the old value
            $old = $session->get($key);
            if ($value != $old) {
                $session->set($key, $value);
            }
        } else {
            $session->set($key, $value);
        }
    }
}
